package com.health.httpbean.format;


import android.os.Environment;
import android.text.TextUtils;

import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.health.db.FDbManager;
import com.health.httpbean.QueryTypeEnum;
import com.health.httpbean.model.HighBloodBean;
import com.health.httpbean.model.MedicineBean;
import com.health.httpbean.modelnew.FaPersonnelHypertensionForm;
import com.health.tablebean.BD_DD_INFO;
import com.health.tablebean.CD_DRUGCONDITION;
import com.health.tablebean.CD_HYPERTENSIONFOLLOWUP;
import com.health.tools.ImageTools;
import com.health.viewscontrols.NovaApplication;
import com.novabluetooth.views.iknetbluetoothlibrary.util.LogUtil;

import net.tsz.afinal.db.sqlite.DbModel;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.health.httpbean.format.FormatUtils.formatAskSymptom;
import static com.health.httpbean.format.FormatUtils.parseDouble;
import static com.health.httpbean.format.FormatUtils.parseInt;
import static com.health.httpbean.format.FormatUtils.parseString;

/**
 * author : xiaoxin
 * date   : 2020/12/16 20:31
 * desc   : 高血压
 * update :
 */
public class FormatGxyData {

    public static HighBloodBean formatGxy(CD_HYPERTENSIONFOLLOWUP gxyModel, List<BD_DD_INFO> list) {

        HighBloodBean highBloodBean = new HighBloodBean();
//        try {
//            //复制档案编号 因为随访表没有存档案编号
//            if (TextUtils.isEmpty(gxyModel.getARCHIVEID())) {
//                String userInfoSql = "SELECT * FROM ARCHIVE_BASEINFO WHERE IDCARD = '" + gxyModel.getIDCARD() + "'";
//                DbModel dbSql = NovaApplication.fdbmanger.findDbSql(userInfoSql);
//                if (dbSql != null) {
//                    highBloodBean.ID_UserDocument = dbSql.getString("ARCHIVEID");
//                }
//            } else {
//                highBloodBean.ID_UserDocument = gxyModel.getARCHIVEID();
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

        try {
            highBloodBean.UserName = gxyModel.getCUSTOMERNAME();
            highBloodBean.BianHao = gxyModel.getCUSTOMERID();
            try {
                highBloodBean.SuiFangRiQi = TimeUtils.millis2String(
                        gxyModel.getFOLLOWUPDATE().getTime(), "yyyy-MM-dd");
            } catch (Exception e) {
                e.printStackTrace();
            }
            //有无转诊   DM01-11_2 有 DM01-11_1 无
            highBloodBean.ZhuanZhenQingKuang = gxyModel.getISREFERRAL() == "DM01-11_2" ? 2 : 1;
            highBloodBean.ZhuanZhenYuanYin = gxyModel.getREFERRALREASON();
            highBloodBean.ZhuanZhenJiGou = gxyModel.getREFERRALORG();
            try {
                highBloodBean.XiaCiSuiFangRiQi = TimeUtils.millis2String(
                        gxyModel.getNEXTFOLLOWUPDATE().getTime(), "yyyy-MM-dd");
            } catch (Exception e) {
                e.printStackTrace();
            }
            //没有医生id
//        highBloodBean.SuiFangYiSheng = gxyModel.getFOLLOWUPDOCTOR();
            String sql = "SELECT * FROM BD_ORGANIZE_EMPLOYEE WHERE USER_ID = '" + gxyModel.getFOLLOWUPDOCTOR() + "'";
            DbModel dbSql = NovaApplication.fdbmanger.findDbSql(sql);
            highBloodBean.SuiFangDoctorName = dbSql.getString("NAME");
            highBloodBean.doctorCardId = dbSql.getString("IDCARD");

            //随访类型、随访分类 :1门诊 2家庭 3电话 哪个是  是不是这里接口多了一个字段
            //DM04-15_1 解析出错
//        highBloodBean.SuiFangLeiXing = parseInt(gxyModel.getFOLLOWUPWAY());
            highBloodBean.SuiFangFangShi = parseInt(gxyModel.getFOLLOWUPWAY());

            //失访原因 没有这个字段
//        highBloodBean.ShiFangYuanYin = gxyModel.getFOLLOWUPDOCTOR();
            //死亡时间 没有这个字段
//        highBloodBean.SiWangShiJian =TimeUtils.millis2String(
//                gxyModel.getNEXTFOLLOWUPDATE().getTime(), "yyyy-MM-dd");

            //ids 是啥 没有找到
//        highBloodBean.IDS_Medicine_ZhiDao = "";
//        highBloodBean.ID_FollowUPTable = "";
//        highBloodBean.GXYID = gxyModel.getID();
            highBloodBean.GXYZhengZhuang = formatAskSymptom(gxyModel.getSYMPTOM(), list);
            highBloodBean.GXYZhengZhuangQiTa = gxyModel.getSYMPTOMOTHER();

            highBloodBean.GXYGaoYa = parseInt(gxyModel.getHYPERTENSION());
            highBloodBean.GXYDiYa = parseInt(gxyModel.getHYPOTENSION());
            highBloodBean.GXYShenGao = parseDouble(gxyModel.getHeight());
            highBloodBean.GXYTiZhong = parseDouble(gxyModel.getWEIGHT());
            highBloodBean.GXYMuBiaoTiZhong = parseDouble(gxyModel.getWEIGHTTARGET());
            highBloodBean.GXYTiZhiZhiShu = parseDouble(gxyModel.getBMI());
            highBloodBean.GXYMuBiaoTiZhiZhiShu = parseDouble(gxyModel.getBMITARGET());
            highBloodBean.GXYXinLv = parseInt(gxyModel.getHEARTRATE());

            highBloodBean.GXYQiTa = gxyModel.getPHYSICALSYMPTOMOTHER();

            highBloodBean.GXYRiXiYanLiang = parseInt(gxyModel.getDAILYSMOKENUM());
            highBloodBean.GXYMuBiaoRiXiYanLiang = parseInt(gxyModel.getDAILYSMOKENUMTARGET());
            highBloodBean.GXYRiYinJiuLiang = parseInt(gxyModel.getDAILYDRINKNUM());
            highBloodBean.GXYMuBiaoRiYinJiuLiang = parseInt(gxyModel.getDAILYDRINKNUMTARGET());
            highBloodBean.GXYYunDongPinLv = parseInt(gxyModel.getSPORTTIMEPERWEEK());
            highBloodBean.GXYYunDongShiChang = parseInt(gxyModel.getSPORTPERMINUTETIME());
            highBloodBean.GXYMuBiaoYunDongPinLv = parseInt(gxyModel.getSPORTTIMESPERWEEKTARGET());
            highBloodBean.GXYMuBiaoYunDongShiChang = parseInt(gxyModel.getSPORTPERMINUTESTIMETARGET());
            highBloodBean.GXYSheYanQingKuang = parseInt(gxyModel.getEATSALTTYPE());
            highBloodBean.GXYMuBiaoSheYanQingKuang = parseInt(gxyModel.getEATSALTTARGET());
            highBloodBean.GXYXinLiTiaoZheng = parseInt(gxyModel.getPSYCHOADJUSTMENT());
            highBloodBean.GXYZunYiXingWei = parseInt(gxyModel.getOBEYDOCTORBEHAVIOR());
            highBloodBean.GXYFuZhuJianCha = gxyModel.getASSISTANTEXAM();
            highBloodBean.GXYFuYaoYiCongXing = parseInt(gxyModel.getMEDICATIONCOMPLIANCE());
            highBloodBean.GXYYaoWuBuLiangFanYingQingKuang = parseInt(gxyModel.getADR());
            highBloodBean.GXYYaoWuBuLiangFanYingMiaoShu = gxyModel.getADR_EX();
            highBloodBean.GXYCiCiSuiFangFenLei = parseInt(gxyModel.getFOLLOWUPTYPE());

            String strWhere = "IDCARD = '" + gxyModel.getIDCARD() + "'";
            List<CD_DRUGCONDITION> drugList = NovaApplication.fdbmanger.findAllByWhere(CD_DRUGCONDITION.class,
                    strWhere + "AND TYPE = '1'");
            List<BD_DD_INFO> unitList = FDbManager.getInstance(NovaApplication.getInstance())
                    .findAllByWhereInInfoDB(BD_DD_INFO.class,
                            "PARENTID='DM04-05'" + " order by cast(SORT as int) asc");
            List<MedicineBean> listMedicine = new ArrayList<>();
            for (int i = 0; i < drugList.size(); i++) {
                if (i == 3) {
                    highBloodBean.GXYYaoWuMingChengQiTa = drugList.get(i).getNAME();
                    highBloodBean.GXYMeiRiCiShuQiTa = parseInt(drugList.get(i).getDAILYTIME());
                    highBloodBean.GXYMeiCiShuLiangQiTa = drugList.get(i).getEVERYTIMEMG();
                } else {
                    MedicineBean medicineBean = new MedicineBean();
                    medicineBean.MedicineName = drugList.get(i).getNAME();
                    medicineBean.Dosage = drugList.get(i).getEVERYTIMEMG();
                    for (int j = 0; j < unitList.size(); j++) {
                        if (drugList.get(i).getRES1() != null
                                && drugList.get(i).getRES1().equals(unitList.get(j).getID())) {
                            medicineBean.Unit = unitList.get(j).getDATAINFO();
                        }
                    }

                    String dailytime = drugList.get(i).getDAILYTIME();
                    if(null != dailytime && !dailytime.equals("") && dailytime.contains("/")){
                        String[] split = dailytime.split("/");
                        if(null != split && !split.equals("")){
                            String s = split[1];
                            if(null != s && !s.equals("")){
                                if(s.equals("99")){
                                    medicineBean.Method = FormatUtils.drugMethod(23);
                                }else{
                                    medicineBean.Method = FormatUtils.drugMethod(Integer.parseInt(s)-1);
                                }
                            }
                            String s1 = split[0];
                            if(null != s1 && !s1.equals("")){
                                if(s1.equals("99")){
                                    medicineBean.Cycle = FormatUtils.cycle(14);
                                }else{
                                    medicineBean.Cycle = FormatUtils.cycle(Integer.parseInt(s1)-1);
                                }
                            }
                        }

                    }
                    listMedicine.add(medicineBean);
                }
            }

            highBloodBean.listMedicine = listMedicine;
            highBloodBean.CardID = gxyModel.getIDCARD();
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        //血型 RH 取那张表
//            highBloodBean.UserBloodGroup = NovaApplication.getUserInfo().getIDCARD();
//            highBloodBean.UserRH = NovaApplication.getUserInfo().getIDCARD();
        return highBloodBean;
    }


    public static FaPersonnelHypertensionForm formatGxyNew(CD_HYPERTENSIONFOLLOWUP gxyModel, List<BD_DD_INFO> list) {

        FaPersonnelHypertensionForm form = new FaPersonnelHypertensionForm();

        try {
            form.sjType = QueryTypeEnum.GXY.getCode();
            form.idCard = gxyModel.getIDCARD();
            form.personalName = gxyModel.getCUSTOMERNAME();
            form.number = gxyModel.getCUSTOMERID();

            String sql = "SELECT * FROM BD_ORGANIZE_EMPLOYEE WHERE USER_ID = '" + gxyModel.getFOLLOWUPDOCTOR() + "'";
            DbModel dbSql = NovaApplication.fdbmanger.findDbSql(sql);
            form.doctorName = dbSql.getString("NAME");
            form.doctorCardId = dbSql.getString("IDCARD");
            form.followTime = gxyModel.getFOLLOWUPDATE();
            form.followType = parseInt(gxyModel.getFOLLOWUPWAY());
            form.symptom = formatAskSymptom(gxyModel.getSYMPTOM(), list);
//            form.symptom = gxyModel.getSYMPTOM();
            form.otherSymptom = parseString(gxyModel.getSYMPTOMOTHER());
            form.bloodPressureNow = gxyModel.getHYPERTENSION() + "/" + gxyModel.getHYPOTENSION();
            form.bloodPressureTarget = gxyModel.getHYPERTENSION() + "/" + gxyModel.getHYPOTENSION();
            form.weight = parseDouble(gxyModel.getWEIGHT());
            form.targetWeight = parseDouble(gxyModel.getWEIGHTTARGET());
            form.bmi = gxyModel.getBMI();
            form.targetBmi = gxyModel.getBMITARGET();
            form.heartRate = gxyModel.getHEARTRATE();
            form.saltUptakeG = gxyModel.getTOSALT();
            form.otherSign = gxyModel.getPHYSICALSYMPTOMOTHER();
            form.dailySmoking = gxyModel.getDAILYSMOKENUM();
            form.targetDailySmoking = gxyModel.getDAILYSMOKENUMTARGET();
            form.dailyDrinking = gxyModel.getDAILYDRINKNUM();
            form.targetDailyDrinking = gxyModel.getDAILYDRINKNUMTARGET();
            form.weeklyExerciseFrequency = gxyModel.getSPORTTIMEPERWEEK();
            form.weeklyExerciseFrequencyT = gxyModel.getSPORTPERMINUTETIME();
            form.targetWeeExeFrequency = gxyModel.getSPORTTIMESPERWEEKTARGET();
            form.targetWeeExeFrequencyT = gxyModel.getSPORTPERMINUTESTIMETARGET();
            form.saltUptake = gxyModel.getEATSALTTYPE();
            form.targetSaltUptake = gxyModel.getEATSALTTARGET();
            form.mindAdjustment = parseInt(gxyModel.getPSYCHOADJUSTMENT());
            form.complianceBehavior = parseInt(gxyModel.getOBEYDOCTORBEHAVIOR());
            form.accessoryExamination = gxyModel.getASSISTANTEXAM();
            form.medicationCompliance = parseInt(gxyModel.getMEDICATIONCOMPLIANCE());
            form.drugAdverseReaction = parseInt(gxyModel.getADR());
            form.drugAdverseReactionText = gxyModel.getADR_EX();
            form.followClassification = parseInt(gxyModel.getFOLLOWUPTYPE());
            form.referralReason = gxyModel.getREFERRALREASON();
            form.referralDepart = gxyModel.getREFERRALORG();
            form.nextTime =  gxyModel.getNEXTFOLLOWUPDATE();
            form.createtime =  gxyModel.getCREATED_DATE();
            form.height =  gxyModel.getHeight();




            StringBuilder drugListStr = new StringBuilder();
            String strWhere = "IDCARD = '" + gxyModel.getIDCARD() + "'";
            List<CD_DRUGCONDITION> drugList = NovaApplication.fdbmanger.findAllByWhere(CD_DRUGCONDITION.class,
                    strWhere + "AND TYPE = '1'");
            List<BD_DD_INFO> unitList = FDbManager.getInstance(NovaApplication.getInstance())
                    .findAllByWhereInInfoDB(BD_DD_INFO.class,
                            "PARENTID='DM04-05'" + " order by cast(SORT as int) asc");
            List<MedicineBean> listMedicine = new ArrayList<>();
            for (int i = 0; i < drugList.size(); i++) {
                MedicineBean medicineBean = new MedicineBean();
                medicineBean.MedicineName = drugList.get(i).getNAME();
                medicineBean.Dosage = drugList.get(i).getEVERYTIMEMG();
                for (int j = 0; j < unitList.size(); j++) {
                    if (drugList.get(i).getRES1() != null
                            && drugList.get(i).getRES1().equals(unitList.get(j).getID())) {
                        medicineBean.Unit = unitList.get(j).getDATAINFO();
                    }
                }

                String dailytime = drugList.get(i).getDAILYTIME();
                if(null != dailytime && !dailytime.equals("") && dailytime.contains("/")){
                    String[] split = dailytime.split("/");
                    if(null != split && !split.equals("")){
                        String s = split[1];
                        if(null != s && !s.equals("")){
                            if(s.equals("99")){
                                medicineBean.Method = FormatUtils.drugMethod(23);
                            }else{
                                medicineBean.Method = FormatUtils.drugMethod(Integer.parseInt(s)-1);
                            }
                        }
                        String s1 = split[0];
                        if(null != s1 && !s1.equals("")){
                            if(s1.equals("99")){
                                medicineBean.Cycle = FormatUtils.cycle(14);
                            }else{
                                medicineBean.Cycle = FormatUtils.cycle(Integer.parseInt(s1)-1);
                            }
                        }
                    }

                }
                //1 维生素C 一日两次 每次1mg 口服;2 维生素C 一日两次 每次1mg 口服
                drugListStr.append((i + 1) + ". " + drugList.get(i).getNAME()
                        + " " + medicineBean.Cycle
                        + " 每次" + drugList.get(i).getEVERYTIMEMG() + medicineBean.Unit
                        + " " + medicineBean.Method + ";"
                );
                listMedicine.add(medicineBean);
            }

            form.medication = drugListStr.toString();
//            String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/report/123.png";
            if (!TextUtils.isEmpty(gxyModel.getRES4())) {
                String base64 = ImageTools.imageToBase64(gxyModel.getRES4());
                form.picBase64 = "data:image/png;base64," + base64;
            }

//            highBloodBean.listMedicine = listMedicine;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return form;
    }


//    public static HdDiseasemanageHypertensionEntity formatGxyNewMySql(CD_HYPERTENSIONFOLLOWUP gxyModel, List<BD_DD_INFO> list) {
//
//        HdDiseasemanageHypertensionEntity form = new HdDiseasemanageHypertensionEntity();
//
//        try {
//            form.visitFollowUpType = QueryTypeEnum.GXY.getCode();
//            form.visitWay = QueryTypeEnum.GXY.getCode();
//            form.cardid = gxyModel.getIDCARD();
//            form.name = gxyModel.getCUSTOMERNAME();
//            form.personalid = gxyModel.getCUSTOMERID();
//
//            String sql = "SELECT * FROM BD_ORGANIZE_EMPLOYEE WHERE USER_ID = '" + gxyModel.getFOLLOWUPDOCTOR() + "'";
//            DbModel dbSql = NovaApplication.fdbmanger.findDbSql(sql);
//            form.resDoctor = dbSql.getString("NAME");
//            form.inputMan = dbSql.getString("NAME");
//            form.inputIdcard = dbSql.getString("IDCARD");
//            form.happentime = TimeUtils.date2String(gxyModel.getFOLLOWUPDATE());
//            form.visitWay = gxyModel.getFOLLOWUPWAY();
//            form.curSymptom = formatAskSymptom(gxyModel.getSYMPTOM(), list);
////            form.symptom = gxyModel.getSYMPTOM();
//            form.otherSymptom = parseString(gxyModel.getSYMPTOMOTHER());
//            form.pressureH = Integer.parseInt(gxyModel.getHYPERTENSION());
//            form.pressureL = Integer.parseInt(gxyModel.getHYPOTENSION());
//            form.bloodPressureTarget = gxyModel.getHYPERTENSION() + "/" + gxyModel.getHYPOTENSION();
//            form.avoirdupois = parseDouble(gxyModel.getWEIGHT());
//            form.targetAvoirdupois = parseDouble(gxyModel.getWEIGHTTARGET());
//            try {
//                form.physique = Double.parseDouble(gxyModel.getBMI());
//                form.targetPhysique = Double.parseDouble(gxyModel.getBMITARGET());
//            } catch (NumberFormatException e) {
//                e.printStackTrace();
//            }
//            form.pulseRate = Integer.valueOf(gxyModel.getHEARTRATE());
//            form.salt = gxyModel.getTOSALT();
//            form.otherSign = gxyModel.getPHYSICALSYMPTOMOTHER();
//            form.dailySmoking = gxyModel.getDAILYSMOKENUM();
//            form.targetDailySmoking = gxyModel.getDAILYSMOKENUMTARGET();
//            form.dailyDrinking = gxyModel.getDAILYDRINKNUM();
//            form.targetDailyDrinking = gxyModel.getDAILYDRINKNUMTARGET();
//            form.weeklyExerciseFrequency = gxyModel.getSPORTTIMEPERWEEK();
//            form.weeklyExerciseFrequencyT = gxyModel.getSPORTPERMINUTETIME();
//            form.targetWeeExeFrequency = gxyModel.getSPORTTIMESPERWEEKTARGET();
//            form.targetWeeExeFrequencyT = gxyModel.getSPORTPERMINUTESTIMETARGET();
//            form.saltUptake = gxyModel.getEATSALTTYPE();
//            form.targetSaltUptake = gxyModel.getEATSALTTARGET();
//            form.mindAdjustment = parseInt(gxyModel.getPSYCHOADJUSTMENT());
//            form.complianceBehavior = parseInt(gxyModel.getOBEYDOCTORBEHAVIOR());
//            form.accessoryExamination = gxyModel.getASSISTANTEXAM();
//            form.medicationCompliance = parseInt(gxyModel.getMEDICATIONCOMPLIANCE());
//            form.drugAdverseReaction = parseInt(gxyModel.getADR());
//            form.drugAdverseReactionText = gxyModel.getADR_EX();
//            form.followClassification = parseInt(gxyModel.getFOLLOWUPTYPE());
//            form.referralReason = gxyModel.getREFERRALREASON();
//            form.referralDepart = gxyModel.getREFERRALORG();
//            form.nextTime =  gxyModel.getNEXTFOLLOWUPDATE();
//            form.createtime =  gxyModel.getCREATED_DATE();
//            form.height =  gxyModel.getHeight();
//
//            StringBuilder drugListStr = new StringBuilder();
//            String strWhere = "IDCARD = '" + gxyModel.getIDCARD() + "'";
//            List<CD_DRUGCONDITION> drugList = NovaApplication.fdbmanger.findAllByWhere(CD_DRUGCONDITION.class,
//                    strWhere + "AND TYPE = '1'");
//            List<BD_DD_INFO> unitList = FDbManager.getInstance(NovaApplication.getInstance())
//                    .findAllByWhereInInfoDB(BD_DD_INFO.class,
//                            "PARENTID='DM04-05'" + " order by cast(SORT as int) asc");
//            List<MedicineBean> listMedicine = new ArrayList<>();
//            for (int i = 0; i < drugList.size(); i++) {
//                MedicineBean medicineBean = new MedicineBean();
//                medicineBean.MedicineName = drugList.get(i).getNAME();
//                medicineBean.Dosage = drugList.get(i).getEVERYTIMEMG();
//                for (int j = 0; j < unitList.size(); j++) {
//                    if (drugList.get(i).getRES1() != null
//                            && drugList.get(i).getRES1().equals(unitList.get(j).getID())) {
//                        medicineBean.Unit = unitList.get(j).getDATAINFO();
//                    }
//                }
//
//                String dailytime = drugList.get(i).getDAILYTIME();
//                if(null != dailytime && !dailytime.equals("") && dailytime.contains("/")){
//                    String[] split = dailytime.split("/");
//                    if(null != split && !split.equals("")){
//                        String s = split[1];
//                        if(null != s && !s.equals("")){
//                            if(s.equals("99")){
//                                medicineBean.Method = FormatUtils.drugMethod(23);
//                            }else{
//                                medicineBean.Method = FormatUtils.drugMethod(Integer.parseInt(s)-1);
//                            }
//                        }
//                        String s1 = split[0];
//                        if(null != s1 && !s1.equals("")){
//                            if(s1.equals("99")){
//                                medicineBean.Cycle = FormatUtils.cycle(14);
//                            }else{
//                                medicineBean.Cycle = FormatUtils.cycle(Integer.parseInt(s1)-1);
//                            }
//                        }
//                    }
//
//                }
//                //1 维生素C 一日两次 每次1mg 口服;2 维生素C 一日两次 每次1mg 口服
//                drugListStr.append((i + 1) + ". " + drugList.get(i).getNAME()
//                        + " " + medicineBean.Cycle
//                        + " 每次" + drugList.get(i).getEVERYTIMEMG() + medicineBean.Unit
//                        + " " + medicineBean.Method + ";"
//                );
//                listMedicine.add(medicineBean);
//            }
//
//            form.medication = drugListStr.toString();
////            String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/report/123.png";
//            if (!TextUtils.isEmpty(gxyModel.getRES4())) {
//                String base64 = ImageTools.imageToBase64(gxyModel.getRES4());
//                form.picBase64 = "data:image/png;base64," + base64;
//            }
//
////            highBloodBean.listMedicine = listMedicine;
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//        return form;
//    }

}
